package com.laijiale.mapper;

import com.laijiale.entities.Room;
import com.laijiale.entities.School;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author JoeZhou
 */
@Repository
public interface SchoolMapper {

    @Insert("insert into rms_school " +
            "(`title`, `college`, `major`, `clazz`, `address`, `info`, " +
            "`deleted`, `created`, `updated`) " +
            "values (#{title}, #{college}, #{major}, #{clazz}, #{address}, #{info}, " +
            "#{deleted}, #{created}, #{updated})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(School school);

    @Select("select t.id, t.title, t.college, t.major, t.clazz, t.address, t.info, " +
            "t.deleted, t.created, t.updated from rms_school t " +
            "where t.id = #{param1} and t.deleted = 0")
    School selectById(Long id);

    @Update("<script>" +
            "update rms_school " +
            "<set> " +
            "<if test='title != null'> title = #{title}, </if> " +
            "<if test='college != null'> college = #{college}, </if> " +
            "<if test='major != null'> major = #{major}, </if> " +
            "<if test='clazz != null'> clazz = #{clazz}, </if> " +
            "<if test='address != null'> address = #{address}, </if> " +
            "<if test='info != null'> info = #{info}, </if> " +
            "<if test='deleted != null'> deleted = #{deleted}, </if> " +
            "<if test='created != null'> created = #{created}, </if> " +
            "<if test='updated != null'> updated = #{updated}, </if> " +
            "</set> " +
            "where id = #{id} and deleted = 0" +
            "</script>")
    int updateById(School school);

    @Update("update rms_school set deleted = 1, updated = now() where id = #{id}")
    int logicDeleteById(Long id);

    @Update("<script>" +
            "update rms_school set deleted = 1, updated = now() where " +
            "<foreach collection='list' item='e' open='id in (' close=')' separator=','>" +
            "${e}" +
            "</foreach>" +
            "</script>")
    int logicDeleteByIds(List<Long> ids);

    @Select("select t.id, t.title, t.college, t.major, t.clazz, t.address, t.info, " +
            "t.deleted, t.created, t.updated from rms_school t " +
            "where t.deleted = 0")
    List<School> list();

    @Delete("delete from rms_school where id = #{param1}")
    int deleteById(Long id);

    @Select("select t.id, t.title, t.college, t.major, t.clazz, t.address, t.info,  " +
            "t.deleted, t.created, t.updated from rms_school t " +
            "where t.deleted = 0 and t.title like concat('%', #{param1}, '%')")
    List<School> listLikeTitle(String title);

}
